package com.springboot.web.controller;

import com.springboot.web.common.CommonResult;
import com.springboot.web.entity.User;
import com.springboot.web.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping(value = "/user/JDBC")
@Slf4j
@RestController
public class UserControllerJDBC {

    @Resource
    private UserService userService;

    //查询所有
    @GetMapping
    public List<User> queryAll () {
        return userService.queryAll();
    }

    //根据名字查询
    @GetMapping("/{name}")
    public List<User> queryByName (@PathVariable String name) {
        return userService.getByName(name);
    }

    //添加用户
    @PostMapping
    public CommonResult add(@RequestBody User user) {
         userService.create(user.getName(),user.getAge());
         return new CommonResult<>(0,"ok",null);
    }

    //删除
    @DeleteMapping
    public CommonResult delete (String name) {
        userService.deleteByName(name);
        return new CommonResult(0,"ok",null);
    }
}
